package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.OrderService;
import com.sky.service.ReportService;
import com.sky.service.WorkspaceService;
import com.sky.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkspaceService {

    @Autowired
    private ReportService reportService;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public BusinessDataVO getBusinessDate(LocalDateTime min,LocalDateTime max) {
//        LocalDate date = LocalDate.now();
//        LocalDateTime max = LocalDateTime.of(date, LocalTime.MAX);
//        LocalDateTime min = LocalDateTime.of(date, LocalTime.MIN);
        Double turnover =  orderMapper.statisticsOrderAmount(Orders.COMPLETED,min, max);
        if (turnover == null){
            turnover = 0.0;
        }
        Integer validOrderCount = orderMapper.statisticsUserNum(Orders.COMPLETED,min,max);
//        TODO
//        什么是有效订单

        Integer totalOrderCount= orderMapper.statisticsUserNum(null,min,max);
        Double orderCompletionRate;
        if(totalOrderCount!=0){
             orderCompletionRate =  ((double)validOrderCount/(double)totalOrderCount);
            orderCompletionRate = Double.parseDouble(String.format("%.2f", orderCompletionRate));
        }else {
             orderCompletionRate =0.0;
        }
//        保留两位小数
        double unitPrice;
        if(validOrderCount!=0){
            unitPrice = turnover/validOrderCount;
            unitPrice = Double.parseDouble(String.format("%.2f", unitPrice));
        }else{
            unitPrice = 0.0;
        }
        LocalDate minDate= min.toLocalDate();
        LocalDate maxDate= max.toLocalDate();
        String newUsersList = reportService.getUserStatistics(minDate,maxDate).getNewUserList();
        int newUsers =0;
        for (String s : newUsersList.split(",")){
            newUsers+=Integer.parseInt(s);
        }
        BusinessDataVO businessDataVO = BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();

        return businessDataVO;
    }

    @Override
    public OrderOverViewVO getOrderOverView() {
        Integer waitingOrders = orderMapper.countByStatus(Orders.TO_BE_CONFIRMED);
        Integer deliveredOrders = orderMapper.countByStatus(Orders.CONFIRMED);
        Integer completedOrders = orderMapper.countByStatus(Orders.COMPLETED);
        Integer cancelledOrders = orderMapper.countByStatus(Orders.CANCELLED);
        Integer allOrders = orderMapper.countByStatus(null);
        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }
    @Override
    public DishOverViewVO getDishOverView() {
        Map sellMap = new HashMap<>();
        sellMap.put("status",1);
        Integer sell = dishMapper.countByMap(sellMap);
        Map unsellMap = new HashMap<>();
        unsellMap.put("status",0);
        Integer unsell = dishMapper.countByMap(unsellMap);

        return DishOverViewVO.builder()
                .discontinued(unsell)
                .sold(sell)
                .build();
    }

    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Map sellMap = new HashMap<>();
        sellMap.put("status",1);
        Integer sell = setmealMapper.countByMap(sellMap);
        Map unsellMap = new HashMap<>();
        unsellMap.put("status",0);
        Integer unsell = setmealMapper.countByMap(unsellMap);


        return SetmealOverViewVO.builder()
                .sold(sell)
                .discontinued(unsell)
                .build();
    }
}
